#include "utils.h"

void allocate_matrix(const int n, matrix & a){
    srand(time(NULL));
    a = new double*[n];
#pragma omp parallel for
    rep(i,0,n)
            posix_memalign((void**)&a[i], 64, n*sizeof(double));
    //a[i] = new double[n];

}

void generate_matrix(const int n, matrix & a){
#pragma omp parallel for
    rep(i,0,n)
            rep(j,0,n)
            a[i][j] = rand()/double(RAND_MAX);
}

void destroy_matrix(const int n, matrix a){
#pragma omp parallel for
    rep(i,0,n)
            delete [] a[i];
    delete [] a;
}

void output(const int n, matrix a, char * filename){
    FILE* f = fopen(filename, "wb");
    rep(i,0,n) {
        fwrite(a[i], n*sizeof(double), 1, f);
    }
    fclose(f);
}

void memset_zero(const int n, matrix a){
#pragma omp parallel for
    rep(i,0,n)
            memset(a[i],0,n*sizeof(double));

}

void input(const int n, matrix a, char * filename){
    FILE* f = fopen(filename, "rb");
    rep(i,0,n) {
        fread(a[i], n*sizeof(double), 1, f);
    }
    fclose(f);
}

void destroy_matrix(double * a){
    delete a;
}

void allocate_matrix(const int n, double *& a){
    a = new double[n*n];
}

void input(const int n, double * a, char * filename){
    FILE* f = fopen(filename, "rb");
    fread(a, n*n*sizeof(double), 1, f);
    fclose(f);
}

void output(const int n, double * a, char * filename){
    FILE* f = fopen(filename, "wb");
    fwrite(a, n*n*sizeof(double), 1, f);
    fclose(f);
}
